<template>
    <div>
        <el-row class="row-with-margin">
            <el-col :span="24">
                <div>
                    <el-button>导出</el-button>
                    <el-button>导入</el-button>
                </div>
            </el-col>
        </el-row>

        <el-row class="row-with-margin">
            <el-col :span="24">
                <div>
                    <!-- 订单状态：
                    <el-select clearable placeholder="请选择订单状态">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select> -->
                    始发地：
                    <el-cascader :options="originOptions" :props="props" clearable placeholder="请选择始发地"
                    v-model="param.dpaAddress3"></el-cascader>
                    目的地：
                    <el-cascader :options="originOptions" :props="props" clearable placeholder="请选择目的地"
                    v-model="param.cdaAddress3"></el-cascader>
                </div>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <el-form :inline="true">
                    <el-form-item label="签收日期">
                        <el-date-picker type="daterange" range-separator="至" start-placeholder="开始日期"
                            end-placeholder="结束日期" v-model="param.betweenTime">
                        </el-date-picker>
                    </el-form-item>

                    <el-form-item label="客户名称：" label-width="100px">
                        <el-input placeholder="请输入关键词搜索" style="width: 200px; margin-left: 10px;" v-model="param.customerName"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="queryAll">查询</el-button>
                        <el-button style="margin-left: 10px;"   @click="onReset">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>

        <!-- 表格部分--------------------------------------------------------------------------------------------------------- -->
        <el-row>
            <el-col :span="24">
                <div>
                    <el-table :data="tableData" style="width: 100%; margin-top: 20px;">
                        <el-table-column type="selection" width="55"></el-table-column>
                        <el-table-column label="操作" width="180">
                            <template v-slot="scope">
                                <el-button type="text" size="medium" @click="jiesuan(scope.row.id)" 
                                :disabled="scope.row.settlementStatus == 1" >
                                    结算</el-button>
                            <el-button type="text" size="medium"  @click="addDuiZhang(scope.row)"
                            :disabled="scope.row.rrId != null "  >生成对账单</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column prop="id" label="应收单号" width="200"></el-table-column>
                        <el-table-column prop="orderId" label="订单号" width="180"></el-table-column>
                        <el-table-column prop="customerName" label="客户名称" width="180"></el-table-column>
                        <el-table-column  label="订单状态" width="180">
                            <template v-slot="scope">
                                <span>
                                    <span v-if="scope.row.orderStatus == 0">待分配</span>
                                    <span v-else-if="scope.row.orderStatus == 1">待调度</span>
                                    <span v-else-if="scope.row.orderStatus == 2">运输中</span>
                                    <span v-else-if="scope.row.orderStatus == 3">已签收</span>
                                    <span v-else-if="scope.row.orderStatus == 4">已拒绝</span>
                                    <span v-else-if="scope.row.orderStatus == 5">已取消</span>
                                    <span v-else-if="scope.row.orderStatus == 6">待提货</span>
                                    <span v-else>未知状态</span>
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="signTime" label="签收时间" width="180"></el-table-column>
                        <el-table-column prop="fahuoAddress" label="发货地址" width="180"></el-table-column>
                        <el-table-column prop="customerAddress" label="收货地址" width="180"></el-table-column>
                        <el-table-column label="结算状态" width="180">
                            <template v-slot="scope">
                                <span v-if="scope.row.settlementStatus == 0">未结算</span>
                                <span v-else-if="scope.row.settlementStatus == 1">已结算</span>
                                <span v-else>未知类型</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="settlementTime" label="结算时间" width="180"></el-table-column>
                        <el-table-column  label="对账单状态" width="180">
                            <template v-slot="scope">
                                <span v-if="scope.row.reconciliationStatus == 0">未生成</span>
                                <span v-else-if="scope.row.reconciliationStatus == 1">已生成</span>
                                <span v-else>未知类型</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="rrId" label="对账单号" width="180"></el-table-column>
                        <el-table-column prop="totalFee" label="运费合计" width="180"></el-table-column>
                        <el-table-column prop="transportationFee" label="运输费" width="180"></el-table-column>
                        <el-table-column prop="insuranceFee" label="保险费" width="180"></el-table-column>
                        <el-table-column prop="packagingFee" label="包装费" width="180"></el-table-column>
                        <el-table-column prop="loadingFee" label="装卸费" width="180"></el-table-column>
                        <el-table-column prop="transferFee" label="中转费" width="180"></el-table-column>
                        <el-table-column prop="serviceFee" label="服务费" width="180"></el-table-column>
                        <el-table-column prop="otherFees" label="其他费用" width="180"></el-table-column>
                        <!-- <el-table-column  label="开票状态" width="180">
                            <template v-slot="scope">
                                <span v-if="scope.row.status == 0">未开票</span>
                                <span v-else-if="scope.row.status == 1">已开票</span>
                                <span v-else>未知类型</span>
                            </template>
                        </el-table-column> -->
                        <el-table-column prop="pickupTime" label="发货时间" width="180"></el-table-column>
                        <el-table-column label="始发地" width="180">
                            <template v-slot="scope">
                                <div>
                                    <span>{{ scope.row.p1Name }}</span>/
                                    <span>{{ scope.row.c1Name }}</span>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="目的地" width="180">
                            <template v-slot="scope">
                                <div>
                                    <span>{{ scope.row.p2Name }}</span>/
                                    <span>{{ scope.row.c2Name }}</span>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="companyName" label="承运商" width="180"></el-table-column>
                        <el-table-column prop="driverName" label="司机" width="180"></el-table-column>
                        <el-table-column prop="contact" label="司机手机号码" width="180"></el-table-column>
                        
                    </el-table>
                </div>
            </el-col>
        </el-row>



        <el-row>
            <el-col :span="24">
                <el-pagination background :page-sizes="[1, 10, 20, 30, 50]" :page-size="pageSize"
                    :current-page="pageNum" :total="total" layout="total,sizes,prev, pager, next,jumper">
                </el-pagination>
            </el-col>
        </el-row>

    </div>
</template>

<script>
export default {
    data() {
        return {
            pageSize: 10,
            pageNum: 1,
            total: 0,
            // options: [{ value: '0', label: '待分配' },
            // { value: '1', label: '待调度' },
            // { value: '2', label: '运输中' },
            // { value: '3', label: '已签收' },
            // { value: '4', label: '已拒绝' },
            // { value: '5', label: '已取消' },
            // { value: '6', label: '待提货' },],
            // value: '',//订单状态下拉框赋值
            originOptions: [],//复制张晨曦的
            props: { value: 'id', label: 'name', children: 'cityList' },  //配置级联选择器多选  还是复制张晨曦的
            tableData: [],//表格对象
            param:{},
        }
    },

    methods: {
        queryOrigin() {
            //查询所有始发地  复制张晨曦的
            this.$axios.get("/api/provincial/queryProvincial")
                .then(resp => {
                    console.log(resp.data)
                    if (resp.data.code == 200) {
                        this.originOptions = resp.data.data
                    }
                })
        },
        queryAll(){
            let pageParam = {};
            pageParam.pageNum = this.pageNum;
            pageParam.pageSize = this.pageSize;
            pageParam.param = this.param;
            this.$axios.post('api/receivable/queryAll', pageParam)
                .then(resp => {
                    console.log(resp.data);
                    if (resp.data.code === 200) {
                        this.tableData = resp.data.data.list
                        this.total = resp.data.data.total
                    }

                })
                .catch(error => {
                    console.error('操作失败:', error);
                    this.$message.error('网络请求失败，请稍后再试');
                });
        },
        jiesuan(id){
            this.$confirm('是否确认完成该单结算', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                
                //点击确定再发送请求
                this.$axios.get("/api/receivable/jiesuan/"+id )
                    .then(resp => {
                        if (resp.data.code === 200) {
                            
                            this.$message({
                                message: '结算成功!!!!',
                                type: 'success'
                            });
                            this.queryAll();
                        }
                        
                    })
                    .catch(error => {
                        console.error('操作失败:', error);
                        this.$message.error('网络请求失败，请稍后再试');
                    });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });
        },
        addDuiZhang(val){
            this.$confirm('是否确认生成对账单', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //点击确定再发送请求
                this.$axios.post("/api/receivable/addDuiZhang",val )
                    .then(resp => {
                        if (resp.data.code === 200) {
                            
                            this.$message({
                                message: '生成成功!!!!',
                                type: 'success'
                            });
                            this.queryAll();
                        }
                        
                    })
                    .catch(error => {
                        console.error('操作失败:', error);
                        this.$message.error('网络请求失败，请稍后再试');
                    });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });
        },
        onReset() {
        // 重置搜索表单
        this.param = {}
        this.queryAll()
      },
    },
    created() {
        this.queryOrigin();
        this.queryAll();
    }
}
</script>

<style scoped>
.row-with-margin {
    margin-bottom: 20px;
    /* 根据需要调整间隙大小 */
}
</style>